//
//  detailPhotoController.m
//  YouGoWeGo
//
//  Created by 范业君 on 14-11-24.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "detailPhotoController.h"
#import "UIImageView+WebCache.h"
#import "detaiTriplModel.h"

@interface detailPhotoController ()<UIScrollViewDelegate>
@property (assign, nonatomic) NSInteger offset;

@end

@implementation detailPhotoController

- (void)dealloc
{
    [_photoArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.photoArray = [NSMutableArray array];
        //NSLog(@"array = %@", _photoArray);
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view  setBackgroundColor: [UIColor redColor]];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationItem setTitle:@"美好回忆"];
    self.offset = 0;
    
    [self createScrollPhotoView];
  
   // [self startAsyncForPhotos];
}

#pragma mark - 
#pragma mark 创建相册ScrollView
- (void)createScrollPhotoView
{
    self.myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height -164)];
    [_myScroll setBackgroundColor:[UIColor cyanColor]];
    [_myScroll setPagingEnabled:YES];
    [_myScroll setShowsHorizontalScrollIndicator:YES];
    [_myScroll setContentSize:CGSizeMake([_photoArray count] * self.view.frame.size.width, 0)];
    [_myScroll setMaximumZoomScale:3.0];
    [_myScroll setMinimumZoomScale:0.5];
    _myScroll.delegate = self;
    
    
    // 创建imageView
    for (int i = 0; i < [_photoArray count]; i ++) {
        UIScrollView *smallScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, _myScroll.frame.size.width, _myScroll.frame.size.height)];
        
        UIImageView *imageView = [[UIImageView alloc] init];
        
        [imageView setBackgroundColor:[UIColor yellowColor]];
        
        detaiTriplModel *model = [_photoArray objectAtIndex:i];
        
        [imageView setImageWithURL:[NSURL URLWithString:model.photo_1600]];
        [imageView setTag:i + 1];
        NSMutableDictionary *dic = model.photo_info;
        
        
        if (dic == nil) {
            [imageView setFrame:CGRectMake(0, 0, self.view.frame.size.width, _myScroll.frame.size.height)];
        } else {
           
            NSNumber *h = [dic objectForKey:@"h"];
            NSNumber *w = [dic objectForKey:@"w"];
            CGFloat h1 = [h floatValue];
            CGFloat w1 = [w floatValue];
            
            CGFloat height = self.view.frame.size.width * h1 / w1;
            [imageView setFrame:CGRectMake(0, 0, self.view.frame.size.width, height)];
        }
    
        imageView.center = _myScroll.center;

        
        [smallScroll setContentSize:CGSizeMake(_myScroll.frame.size.width, 0)];
        smallScroll.delegate = self;
        [smallScroll setMaximumZoomScale:3.0];
        [smallScroll setMinimumZoomScale:0.5];
        [smallScroll addSubview:imageView];
        [_myScroll addSubview:smallScroll];
        [smallScroll release];
        [imageView release];
    }
    [self.view addSubview:_myScroll];
    [_myScroll release];
}


// 告诉那张图片需要放大或者缩小
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    for (UIView *v in scrollView.subviews){
        return v;
    }
    return nil;
}

// 图片的还原
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.myScroll){
        CGFloat x = scrollView.contentOffset.x;
        if (x==_offset){
            
        }
        else {
            _offset = x;
            for (UIScrollView *s in scrollView.subviews){
                if ([s isKindOfClass:[UIScrollView class]]){
                    [s setZoomScale:1.0];
                    
                }
            }
        }
    }

    
}

























@end
